package com.app.buss.ui;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.app.buss.R;
import com.app.buss.adapter.CommentAdapter;
import com.app.buss.base.BaseActivity;
import com.app.buss.bean.CommentDetailBean;
import com.app.buss.bean.TopicBean;
import com.app.buss.mvp.presenter.MainPresenter;
import com.app.buss.mvp.view.IMainView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CommentActivity extends BaseActivity<MainPresenter> implements IMainView {


    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.tv_des)
    TextView tvDes;
    @BindView(R.id.rv_comment)
    RecyclerView rvComment;
    @BindView(R.id.detail_page_do_comment)
    EditText detailPageDoComment;
    @BindView(R.id.img_report)
    ImageView imgReport;
    @BindView(R.id.tv_report)
    TextView report;
    private CommentAdapter adapter;
    private List<CommentDetailBean> list = new ArrayList<>();

    @Override
    protected MainPresenter createPresenter() {
        return new MainPresenter(this);
    }

    @Override
    protected int bindContent() {
        return R.layout.aty_chat_detail;
    }

    @Override
    protected void initSome() {
        initToolBar("Details");
        initTopic();
        adapter = new CommentAdapter(list, this);
        rvComment.setLayoutManager(new LinearLayoutManager(this));
        rvComment.setAdapter(adapter);
    }

    private void initTopic() {
        if (getIntent() != null && getIntent().getExtras() != null) {
            TopicBean bean = (TopicBean) getIntent().getExtras().getSerializable("data");
            if (bean != null) {
                tvName.setText(bean.getName());
                tvDes.setText(bean.getDes());
                tvTime.setText(bean.getTime());
                if(bean.getChats()>0){
                    if(bean.getChats()==1){
                        list.add(new CommentDetailBean("2020.11.11 10:20",2,"Totally agree.","James Plan B"));
                    }else if(bean.getChats()==2){
                        list.add(new CommentDetailBean("2020.11.10 13:10",1,"hahahaaaaa,i also want to know.","Spider-Man"));
                        list.add(new CommentDetailBean("2020.11.12 15:33",0,"secret","Robinson"));
                    }if(bean.getChats()==3){
                        list.add(new CommentDetailBean("2020.11.11 09:12",1,"There are too few people.","Cupid"));
                        list.add(new CommentDetailBean("2020.11.11 22:11",0,"Do you all like to talk about such useless questions,man?","Robinson"));
                        list.add(new CommentDetailBean("2020.11.13 10:55",1,"YoYOdsadhasdkas","Zeus"));
                    }
                }
            }
        }
    }

    @Override
    public void getDataSuccess(Object model) {
    }

    @Override
    public void getDataFail(String msg) {
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.tv_report, R.id.tv_send})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_report:
                toastShow("Spam reported");
                report.setEnabled(false);
                imgReport.setImageDrawable(getDrawable(R.drawable.ic_report_green));
                break;
            case R.id.tv_send:
                if(!TextUtils.isEmpty(detailPageDoComment.getText().toString())){
                    toastShow("Comment successful!");
                    detailPageDoComment.setText("");
                    InputMethodManager manager = ((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE));
                    if (manager != null)
                        manager.hideSoftInputFromWindow(view.getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm");
                    list.add(new CommentDetailBean(simpleDateFormat.format(new Date()),0,detailPageDoComment.getText().toString(),"Hero123"));
                    adapter.notifyDataSetChanged();
                }else {
                    toastShow("Comment cannot be empty.");
                }
                break;
        }
    }
}
